/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.beans;

import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttribute;
import com.iti.jets.gpts.service.ViewTransServiceTypeAtt;
import com.sun.jersey.api.client.UniformInterfaceException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.codehaus.jettison.json.JSONException;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author noha
 */
@ManagedBean(name = "uuu")
@ViewScoped
public class TransportationServiceTypeBean implements Serializable {
DataModel<TranServiceTypeAttribute> TSattributeTypesModel;
    int tranid;
    int attid;
    String TSTName;
    String ConfirmEditName;

    public String getConfirmEditName() {
        return ConfirmEditName;
    }

    public void setConfirmEditName(String ConfirmEditName) {
        this.ConfirmEditName = ConfirmEditName;
    }

    public String getTSTName() {
        return TSTName;
    }

    public void setTSTName(String TSTName) {
        this.TSTName = TSTName;
    }

    public int getAttid() {
        return attid;
    }

    public void setAttid(int attid) {
        this.attid = attid;
    }

    public DataModel<TranServiceTypeAttribute> getTSattributeTypesModel() {
        return TSattributeTypesModel;
    }

    public void setTSattributeTypesModel(DataModel<TranServiceTypeAttribute> TSattributeTypesModel) {
        this.TSattributeTypesModel = TSattributeTypesModel;
    }

    public int getTranid() {
        System.out.println("iddddget" + tranid);
        return tranid;
    }

    public void setTranid(int tranid) {
        this.tranid = tranid;
    }
    
    public void loadtst(SelectEvent event){ 
        TranServiceType stst = (TranServiceType)  event.getObject();
        System.out.println("khalifa khalifa khalifa khalifa khalifa khalifa khalfia ");
       if(stst!=null){
       tranid = stst.getId(); 
         System.out.println("hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"+tranid);
        ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
        try {
            int id = getTranid();
            System.out.println("ya idddd"+getTranid());
            TSattributeTypesModel = new ListDataModel(tst.getTranServiceTypeIndex(id));
        } catch (UniformInterfaceException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
       }
       
       
       
    }

    public TransportationServiceTypeBean() {

    }

    public void editName() {
        String newname = getTSTName();
        int id = getTranid();
        ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
      String y=tst.setTransportationTypeName(id, newname);
        System.out.println("yyyyyyyyyyyyyyyy"+y);
      if (y.equals("editnamedone"))
      {setConfirmEditName("Name Edit Successfully");
          System.out.println("confirm"+ConfirmEditName);}else{setConfirmEditName("Name Doesnot Edit Successfully");}   
    }

    public void showMessage() {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error Message", "This Attribute Already Added");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }

    public void showMessageConfirmation() {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Confirm", "Done");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }

    public void onEdit(RowEditEvent event) {
        System.out.println("da5al aleditttttttttttttttttttttttttttttttt");
        FacesMessage msg = new FacesMessage("attribute Edited", ((TranServiceTypeAttribute) event.getObject()).getAttributeType().getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        TranServiceTypeAttribute TSTAEdited = TSattributeTypesModel.getRowData();
        System.out.println("atttttttttt name" + TSTAEdited.getAttributeType().getName());
        // int m=TSTAEdited.getAttributeType().getId();
        //      TSTAEdited.getId().setIdAttributeType(m);
        //    TSTAEdited.getId().setIdTranServiceType(getTranid());
        //System.out.println("toooooooot"+"id al attribute"+TSTAEdited.getId().getIdAttributeType());
        System.out.println("idaltransportation" + getTranid() + "value " + TSTAEdited.getValue() + " name " + TSTAEdited.getAttributeType().getName() + " " + "idalattribute" + TSTAEdited.getAttributeType().getId() + " is Updated Value...............................");
        ViewTransServiceTypeAtt tstService = new ViewTransServiceTypeAtt();
        // int attid=tstService.getAttributeID(TSTAEdited.getAttributeType().getName());
        tstService.UpdateTransSerTypeAtt(TSTAEdited.getAttributeType().getId(), getTranid(), TSTAEdited.getAttributeType().getName(), TSTAEdited.getValue());
    }

    public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Car Cancelled", ((TranServiceTypeAttribute) event.getObject()).getAttributeType().getName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
        System.err.println("OnCancel Hereeeeeeeeeeeeeeeeeee");

    }

}
